const Koa = require('koa');
const Router = require('koa-Router')
const { koaBody } = require('koa-body')
const bodyparser = require('koa-bodyparser')
const cors = require('@koa/cors')
const Mock = require('mockjs')
const OSS = require('ali-oss')
const client = new OSS({
    // yourregion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
    region: 'oss-cn-beijing',
    // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    accessKeyId: "LTAI5tGuaNqVodDJYZgR31tE",
    accessKeySecret: "LLpHFbWtuQjPWw3JK2TChy1BvqWD1I",
    // 填写Bucket名称。
    bucket: 'chenyan526',
});

var jwt = require('jsonwebtoken');
const koaJwt = require('koa-jwt');
// 为你推荐数据渲染
const TabsoneData = Mock.mock({
    'list|25': [
        {
            'id|+1': 1,
            'image|1': ['https://pub-cdn-oss.chuangkit.com/ad_position/6e54a105b22b4c71a88e5c83908da221', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/21/8beee56d-5edc-4ed3-b2e9-9496fbe863c8_thumb?v=1726121404000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/22/f430e114-b69e-443d-8b35-21496aee87eb_thumb?v=1724914201000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/cd88199f-05e5-4744-9e9f-340501ac0a47_thumb?v=1722395040000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2023/08/23/0843c6f8-fa37-4a45-a586-5deec5338a7b_thumb?v=1725517200000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp']
        }
    ]
})
// 中秋
const TabstwoData = Mock.mock({
    'list|25': [
        {
            'id|+1': 1,
            'image|1': ['https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/16/43b46e34-d1a8-4005-b556-8bc8b0cb955e_thumb?v=1723794121000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/21/1173f10c-2e38-4319-b0a7-87f341d7b148_thumb?v=1724203921000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/c4db7baf-cf5f-4176-93df-f5c92206f96b_thumb?v=1722405605000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2023/07/27/0125710f-c1ea-417a-9850-e439105954be_thumb?v=1690422247000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/233b44f3-d3b9-4487-8743-e129e0ccf962_thumb?v=1722405603000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp']
        }
    ]
})

// 喜报
const TabsthreeData = Mock.mock({
    'list|25': [
        {
            'id|+1': 1,
            'image|1': ['https://pub-cdn-oss.chuangkit.com/designTemplate/2023/11/20/b028f2cb-7582-4d04-8d8c-26433a8c1891_thumb?v=1720750200000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/15/7546858f-68de-4ec7-9696-536808d0f82b_thumb?v=1721109901000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/74820835-fb2b-44a6-9acc-c74158620fb5_thumb?v=1722415443000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2022/12/28/7643c981-21c7-4be5-8a1b-2364f0ebf761_thumb?v=1717815291000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/16/df0847bd-f2b8-4ece-8bc5-dfe4408a3f94_thumb?v=1721097722000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp']
        }
    ]
})

// 热门推荐
const HotData = Mock.mock({
    'list|25': [
        {
            'id|+1': 1,
            'image|1': ['https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/27/32ca1d79-dab5-4df6-a303-587dfbb81ac2_thumb?v=1724724960000&x-oss-process=image%2Fresize%2Cw_400%2Fsharpen%2C100%2Fformat%2Cwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2023/08/19/b9e9a8c2-a8c6-4ba3-8096-383bcab08379_thumb?v=1692375901000&x-oss-process=image%2Fresize%2Cw_400%2Fsharpen%2C100%2Fformat%2Cwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/26/96c0e803-eeb4-437f-9ae9-fc845f6a2657_thumb?v=1721980082000&x-oss-process=image%2Fresize%2Cw_400%2Fsharpen%2C100%2Fformat%2Cwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/30/f6f8d928-1c6a-454b-ab74-60d1c4defd13_thumb?v=1724982242000&x-oss-process=image%2Fresize%2Cw_400%2Fsharpen%2C100%2Fformat%2Cwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/30/8091832e-0124-4e39-b95c-5104e168351b_thumb?v=1724984521000&x-oss-process=image%2Fresize%2Cw_400%2Fsharpen%2C100%2Fformat%2Cwebp', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/16/63a2c43d-b9b2-451c-8ea9-d15ba3b9f4a7_thumb?v=1724737507000&x-oss-process=image%2Fresize%2Cw_400%2Fsharpen%2C100%2Fformat%2Cwebp']
        }
    ]
})

// 最新上传
const NewData = Mock.mock({
    'list|25': [
        {
            'id|+1': 1,
            'image|1': ['https://pub-cdn-oss.chuangkit.com/designTemplate/2024/09/13/bb0ed1d0-f314-4d81-8423-30ef237b533c_thumb?v=1726217400000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/09/13/412182fb-fd19-4330-9683-9dbae857c631_thumb?v=1726237920000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/09/13/2d1de23a-4c4e-43fb-bb3c-4113f6422738_thumb?v=1726217160000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/09/13/edcd6034-2dab-4b99-910b-799832644ae1_thumb?v=1726216683000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/09/13/8c84b8ed-ae90-4063-b5c9-a6afc24eb0eb_thumb?v=1726216680000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/09/13/fb58d236-bbe9-4d9c-8544-5f55f77cceaf_thumb?v=1726213800000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70']
        }
    ]
})

// 最多使用
const ManyData = Mock.mock({
    'list|25': [
        {
            'id|+1': 1,
            'image|1': ['https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/24bbaea4-b19c-41bf-94d6-0e24e6c704f4_thumb?v=1726019403000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/eb18cbfc-dd9e-4a81-b290-f86c8748633b_thumb?v=1723011603000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/07/31/a0de18eb-86f8-4028-8470-04f553c28b02_thumb?v=1724982602000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/30/6b1f97c4-ef4a-4b51-9a8c-81a5e31a96bd_thumb?v=1725464706000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/09/23c1f744-d3dc-4930-aaeb-62a079f5c4ae_thumb?v=1723196400000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70', 'https://pub-cdn-oss.chuangkit.com/designTemplate/2024/08/16/8e8f2fd7-a7f7-4db9-bb53-d0e03bc7cfb5_thumb?v=1723801684000&imageMogr2%2Fthumbnail%2F400x%3E%2Fformat%2Fwebp%2Fsharpen%2F70']
        }
    ]
})


const app = new Koa();
const router = new Router()

app.use(cors({ origin: '*' }))
app.use(koaBody({ multipart: true }))
app.use(bodyparser())

app.use(router)

const secret = 'secret'; // 定义你的密钥
app.use(koaJwt({ secret }).unless({ path: [/^\/api\/login/] }));

app.use(async (ctx, next) => {
    try {
        await next();
    } catch (err) {
        if (err.status === 401) {
            ctx.status = 401;
            ctx.body = { error: 'Token expired or invalid' };
        } else {
            throw err;
        }
    }
});


// 为你推荐
router.get('/TabsoneData', ctx => {
    ctx.body = TabsoneData
})
// 中秋
router.get('/TabstwoData', ctx => {
    ctx.body = TabstwoData
})
// 喜报
router.get('/TabsthreeData', ctx => {
    ctx.body = TabsthreeData
})
// 热门推荐
router.get('/HotData', ctx => {
    ctx.body = HotData
})

// 最新上传
router.get('/NewData', ctx => {
    ctx.body = NewData
})

router.get('/ManyData', ctx => {
    ctx.body = ManyData
})

const data = [
    {
        'user': '17696007626',
        'pas': '123456'
    }
];

// 登录接口
router.post('/api/login', ctx => {
    const { username, password } = ctx.request.body;
    const flag = data.find(item => item.user === username && item.pas === password);

    if (flag) {
        const token = jwt.sign({ data: username }, secret, { expiresIn: '30m' }); // 设置token过期时间为30分钟
        const userimg = 'https://img1.baidu.com/it/u=1653751609,236581088&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1726851600&t=b30497017045126591cbdd053ed18e49';
        ctx.body = {
            code: 200,
            msg: '登录成功',
            token,
            userimg
        };
    } else {
        ctx.body = {
            code: 400,
            msg: '登录失败'
        };
    }
});
const data1 = Mock.mock({ 
    'list|100': [
        '@ctitle(10,20)'
    ]
})
router.get('/api/list', ctx => {
    ctx.body = data1.list
})
// 上传
router.post('/api/up', async (ctx) => {
    let files = ctx.request.files;
    console.log(files);
    try {
        // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
        // 如果本地文件的完整路径中未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
        let arr = []
        for (let key in files) {
            const result = await client.put(files[key].originalFilename, files[key].filepath);
            arr.push(result.url)
        }
        ctx.body = ({
            code: 200,
            msg: '上传成功', 
            arr: arr
        })
        // console.log(result);
    } catch (e) {
        console.log(e);
    }
})
const chongzhi = Mock.mock({
    'list|30': [
        {
            'id|+1': 1,
            'name': '@cname',
            'title': "@ctitle(3,4)",
            'phone': '11122233345',
            'dan': '1287361278312735',
            'yewu|1': ['充值', '订单消费', '退款'],
            'liushui|1': ['收入', '支出'],
            'time': new Date(),
            "qian|1-2000": 20
        }
    ]
})
router.get('/api/chong', ctx => {
    ctx.body = chongzhi.list
})
// 模版
const moban = Mock.mock({
    'list|20': [
        {
            'id|+1': 1,
            'desc': "@ctitle(30,35)",
            'price|100-200': 100.00,
            'liang|1000-10000': 1000,
            'title|1': ['老郭记自营店', '果行经营二部'],
            'time': new Date(),
            'img': "@image(20x20,@color)",
            'status|1': [false, true]
        }
    ]
})
router.get('/api/moban', ctx => {
    ctx.body = moban.list
})
// 订单
const dingdan = Mock.mock({
    'list|20': [
        {
            'desc': "@ctitle(30,35)",
            'type|1': ['代付款', '待发货', '代收款', '已完成', '已关闭'],
            'price|100-200': 100.00,
            'title': '@ctitle(5,10)',
            'name': '@cname',
            'phone': '16677778888',
            'img': '@image(20x20,@color)',
            'city': '@city()',
            'bianhao|100000000000000-999999999999999': 100000000000001
        }
    ]
})
router.get('/api/dingdan', ctx => {
    ctx.body = dingdan.list
})
app.listen(3009, () => {
    console.log('服务启动成功3009')
});